# xpath 安装 pip install lxml

# 教程 https://juejin.cn/post/7019567591106543646?searchId=202312201001208BC73720CE97B9674F2A
from lxml import etree
import requests


headers = {
    "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"
}
url = "https://www.baidu.com/"
response = requests.get(url=url,headers=headers)
#使用etree进行解析
data = etree.HTML(response.text)

#可参考上表格进行对比，//div可理解为任意路径下的一个div标签，@class表示选取class属性，text()表示获取text文本
name = data.xpath("//div[@class='title-text c-font-medium c-color-t']/text()")
print(name[0])


